﻿using Masuit.Tools;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Simple.Common.Helpers
{
    /// <summary>
    /// 身份证号码帮助类
    /// </summary>
    public static class IdNumberHelper
    {
        /// <summary>
        /// 验证身份证
        /// </summary>
        /// <param name="idNumber"></param>
        /// <returns></returns>
        public static bool CheckIdNumberFormat(string idNumber)
        {
            return idNumber.Length == 18;
        }

        /// <summary>
        /// 提取出生年月日
        /// </summary>
        /// <param name="idNumber"></param>
        /// <returns></returns>
        public static DateTime GetIdNumberBirthday(string idNumber)
        {
            string birthYear = idNumber.Substring(6, 4);
            string birthMonth = idNumber.Substring(10, 2);
            string birthDay = idNumber.Substring(12, 2);
            return DateTime.Parse($"{birthYear}-{birthMonth}-{birthDay}");
        }

        /// <summary>
        /// 提取性别信息，奇数为男，偶数为女
        /// </summary>
        /// <param name="idNumber"></param>
        /// <returns>男 = 1, 女 = 2</returns>
        public static int GetIdNumberGender(string idNumber)
        {
            char genderCode = idNumber[16]; // 性别码位于第17位（索引从0开始）
            int gender = (genderCode % 2 == 0) ? 2 : 1;
            return gender;
        }

        /// <summary>
        /// 获取属相信息
        /// </summary>
        /// <param name="brithday"></param>
        /// <returns></returns>
        public static string GetChineseZodiac(string idNumber)
        {
            var birthday = GetIdNumberBirthday(idNumber);
            ChineseLunisolarCalendar chineseLunisolarCalendar = new ChineseLunisolarCalendar();
            int year = chineseLunisolarCalendar.GetSexagenaryYear(birthday);
            string treeYear = "鼠牛虎兔龙蛇马羊猴鸡狗猪";
            string chineseZodiac = treeYear.Substring(chineseLunisolarCalendar.GetTerrestrialBranch(year) - 1, 1);
            return chineseZodiac;
        }
    }
}
